# Maintainer: Lukas Jirkovsky <l.jirkovsky@gmail.com>
# Maintainer: Bruno Pagani <archange@archlinux.org>

pkgname=ispc
_commit=41549e2786f056c69a5a9d09f44ccc958bba2660  # refs/tags/v1.19.0
pkgver=1.19.0
pkgrel=2
pkgdesc="Compiler for high-performance SIMD programming on the CPU"
arch=(loong64 x86_64)
url="https://ispc.github.io/"
license=(BSD)
depends=(clang gcc-libs glibc llvm-libs spirv-llvm-translator) #ncurses zlib
makedepends=(cmake git level-zero-headers level-zero-loader llvm openmp python vc-intrinsics)
checkdepends=(intel-compute-runtime)
optdepends=(
    'intel-compute-runtime: GPU support'
    'level-zero-loader: GPU support'
    'openmp: GPU support'
)
# NOTE: libispcrt_static.a is required by ospray during build time
options=(staticlibs)
source=(
  git+https://github.com/$pkgname/$pkgname#tag=$_commit
  $pkgname-benchmark::git+https://github.com/google/benchmark.git
  $pkgname-googletest::git+https://github.com/google/googletest.git
)
sha256sums=('SKIP'
            'SKIP'
            'SKIP')

pkgver() {
  cd $pkgname
  git describe --tags | sed 's/^v//;s/\([^-]*-g\)/r\1/;s/-/./g'
}

prepare() {
  cd $pkgname
  git submodule init
  git config submodule.benchmarks/vendor/google/benchmark.url "$srcdir/$pkgname-benchmark"
  git config submodule.ispcrt/tests/vendor/google/googletest.url "$srcdir/$pkgname-googletest"
  git -c protocol.file.allow=always submodule update
}

build() {
  local cmake_options=(
    -B build
    -D CMAKE_BUILD_TYPE=Release  # None not allowed :(
    -D CMAKE_INSTALL_PREFIX=/usr
    -D ISPC_INCLUDE_EXAMPLES=OFF
    -D ISPC_OPAQUE_PTR_MODE=OFF  # suggested in https://github.com/ispc/ispc/issues/2427
    -D ISPC_STATIC_LINK=OFF
    -D XE_ENABLED=ON
    -D XE_DEPS_DIR=/usr
    -S $pkgname
    -W no-dev
  )

  cmake "${cmake_options[@]}"
  cmake --build build --verbose
}

check() {
  # https://github.com/ispc/ispc/issues/2427
  # https://github.com/ispc/ispc/issues/2428
  # https://github.com/ispc/ispc/issues/2429
  make -C build check-all || echo "Tests failed"
}

package() {
  DESTDIR="$pkgdir" cmake --install build
  install -Dm644 $pkgname/LICENSE.txt -t "${pkgdir}"/usr/share/licenses/${pkgname}
  # Remove uneeded files: https://github.com/ispc/ispc/issues/2482
  rm -rv "${pkgdir}"/build
}
